#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int T, n, a[234567], lst[1234567];
struct score{
    int s, i, j, c=1;
    bool operator< (const score b)const{
        return i<b.i;
    }
}sc[234567];
score stk[234567];
int snt;
void mian(){
    scanf("%d", &n);
    for(int i=1;i<=n;i++){
        scanf("%d", &a[i]);
        lst[a[i]] = 0;
    }
    snt = 0;
    for(int i=1;i<=n;i++){
        if(lst[a[i]]){
            snt++;
            sc[snt] = {a[i], lst[a[i]], i};
        }
        lst[a[i]] = i;
    }
    sort(sc+1, sc+snt+1);
    // for(int i=1;i<=snt;i++){
    //     printf("s=%d (%d, %d)\n", sc[i].s, sc[i].i, sc[i].j);
    // }
    if(snt == 0){
        printf("0\n");
        return;
    }
    if(snt == 1){
        printf("%d\n", sc[1].s);
        return;
    }
    int ans = 0;
    score now = sc[1];
    /*for(int i=2;i<=snt;i++){
        if(now.j<=sc[i].i){//Separate|Cut
            now = {now.s+sc[i].s, now.i, sc[i].j, max(now.c, s[i].c)};
        }else if(now.i<sc[i].i&&now.j<sc[i].j){
            now = {max(now.s, sc[i].s), now.i, now.j, m}
        }
    }*/
    for(int i=1;i<=snt;i++){
        ans += sc[i].s;
    }
    printf("%d\n", ans);
}
int main(){
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    scanf("%d", &T);
    while(T--){
        mian();
    }
    return 0;
}
//g++ x.cpp -o x -O2 -std=c++14 -Wall -Wshadow